const shareUtil = require('../../../utils/shareUtil.js');
const app = getApp()
const imgUrl =app.globalData.imgUrl
!function() {
  getApp();
  var t = new (require("../utils/js/waterMark.js"))(), a = {
      colorMap: [ [ "0", "#ffffff" ], [ "1", "#979797" ], [ "2", "#000000" ], [ "3", "#ee0000" ], [ "4", "#f08600" ], [ "5", "#035aff" ], [ "6", "#00FF00" ] ],
      waterMarkConfig: {
          color: "#ffffff",
          opacity: .5
      },
      text: "请输入文字",
      debounceDelay: 400
  }, e = getApp();
  Page({
      data: {
          app: "",
          bool: false,
          canvasW: 100,
          canvasH: 200,
          inputFocus: false,
          currentColorIndex: -1,
          ad: {},
          imgUrl:imgUrl
      },
      showMask: function() {
          this.setData({
              hasMask: true,
              bool: true
          });
      },
      hideMask: function() {
          this.setData({
              hasMask: false,
              bool: false
          });
      },
      onLoad: function() {
          this.setData({
              ad: e.globalData.ad
          });
      },
      test: function(t) {
          var a = t.currentTarget.dataset;
          wx.navigateToMiniProgram({
              appId: a.id,
              path: "",
              extraData: {
                  foo: "bar"
              },
              envVersion: "release",
              success: function(t) {
                  console.log(t);
              }
          });
      },
      onReady: function() {
          this.getDefaultConfig();
      },
      touchImgstart: function(t) {
          this.setData({
              touch: t.changedTouches[0],
              touchStartTime: new Date()
          });
      },
      touchImgend: function(t) {
          var a = Math.abs, e = this.data.touch, n = t.changedTouches[0], o = this.data.touchStartTime;
          350 <= new Date() - o || 10 > a(e.clientX - n.clientX) && 10 > a(e.clientY - n.clientY) && this.chooseImg();
      },
      chooseImg: function() {
          var e = this;
          wx.chooseImage({
              count: 1,
              success: function(n) {
                  wx.getImageInfo({
                      src: n.tempFilePaths[0],
                      success: function(o) {
                          var i = o.width, s = o.height, c = wx.getSystemInfoSync().windowWidth, r = c * s / i;
                          e.setData({
                              canvasW: c,
                              canvasH: r,
                              imgW: i,
                              imgH: s
                          });
                          var u = {
                              text: e.data.text,
                              id: "myCanvas",
                              color: "#ffffff",
                              xStart: 0,
                              yStart: -.71 * o.width,
                              xSpace: 60,
                              ySpace: 60,
                              rotate: 45,
                              opacity: .5,
                              width: c,
                              height: r,
                              scale: c / i,
                              size: 30,
                              imgUrl: n.tempFilePaths[0]
                          };
                          u = Object.assign(u, a.waterMarkConfig), t.mark(u).then(function() {
                              e.log(1), e.setData({
                                  draw: true
                              }), -1 === e.data.currentColorIndex && -1 !== e.data.defaultColorIndex && e.setData({
                                  currentColorIndex: e.data.defaultColorIndex
                              });
                          });
                      }
                  });
              }
          });
      },
      saveImg: function() {
          return !!this.data.draw && (wx.showLoading({
              title: "保存中"
          }), wx.canvasToTempFilePath({
              canvasId: "myCanvas",
              destWidth: this.data.imgW,
              destHeight: this.data.imgH,
              success: function(t) {
                  wx.saveImageToPhotosAlbum({
                      filePath: t.tempFilePath,
                      success: function() {
                          wx.hideLoading(), wx.showToast({
                              title: "已保存到相册",
                              icon: "success",
                              duration: 2e3
                          });
                      }
                  });
              }
          }), void this.log(3));
      },
      previewImg: function() {
          var t = this;
          console.log(t.data.text);
          wx.showLoading({
            title: "加载中"
          }), 
          wx.canvasToTempFilePath({
            canvasId: "myCanvas",
            destWidth: t.data.imgW,
            destHeight: t.data.imgH,
            success: function(t) {
                wx.hideLoading(),
                wx.navigateTo({
                    url: "/pages/subimg/preview/preview?imageurl=" + t.tempFilePath
                });
            }
          })
      },
      handleCircleClick: function(e) {
          var n = e.target.id;
          if (n) {
              var o = this;
              this.setData({
                  currentColorIndex: n
              }, function() {
                  return a.waterMarkConfig.color = o.data.ColorMap[o.data.currentColorIndex][1], !!o.data.draw && (t.reRendering({
                      color: a.waterMarkConfig.color
                  }), void o.log(1));
              });
          }
      },
      handleTextChange: function(t, a) {
          var e, n, o, i, s, c = function c() {
              var r = new Date().getTime() - i;
              r < a && 0 <= r ? e = setTimeout(c, a - r) : (e = null, s = t.apply(o, n), e || (o = n = null));
          };
          return function() {
              return o = this, n = arguments, i = new Date().getTime(), e || (e = setTimeout(c, a)), 
              s;
          };
      }(function(a) {
          this.data.draw && t.reRendering({
              text: a.detail.value
          });
      }, a.debounceDelay),
      handleTextFocus: function() {
          this.setData({
              inputFocus: true
          });
      },
      handleTextBlur: function(t) {
          var a = this.data.text, 
          e = this;
          this.setData({
              inputFocus: false,
              text: t.detail.value
          }, function() {
              e.data.draw && a !== e.data.text && e.log(1);
          });
      },
      log: function(t) {
          console.log(t);
      },
      getDefaultConfig: function() {
          var t = a.colorMap, e = a.waterMarkConfig, n = t.filter(function(t) {
              return t[1] == e.color;
          });
          this.setData({
              ColorMap: t,
              defaultColorIndex: n.length ? n[0][0] : -1,
              defaultText: a.text,
              text: a.text
          });
      },
      onShareAppMessage:shareUtil.shareConfig,
      onShareTimeline:shareUtil.shareConfig,
      onLongTrap: function() {
          var t = this;
          wx.showActionSheet({
              itemList: [ "预览", "保存" ],
              success: function(a) {
                  console.log(a.tapIndex), 0 == a.tapIndex && t.previewImg(), 1 == a.tapIndex && t.saveImg();
              },
              fail: function(t) {
                  console.log(t.errMsg);
              }
          });
      }
  });
}();